/*
 * Copyright (c) 2022-2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "continuous_task_callback_info.h"
#include "string_ex.h"
#include "continuous_task_log.h"
#include "ipc_util.h"

namespace OHOS {
namespace BackgroundTaskMgr {
ContinuousTaskCallbackInfo::ContinuousTaskCallbackInfo() {}

uint32_t ContinuousTaskCallbackInfo::GetTypeId() const
{
    return typeId_;
}

const std::vector<uint32_t>& ContinuousTaskCallbackInfo::GetTypeIds() const
{
    return typeIds_;
}

bool ContinuousTaskCallbackInfo::IsBatchApi() const
{
    return isBatchApi_;
}

int32_t ContinuousTaskCallbackInfo::GetAbilityId() const
{
    return abilityId_;
}

int32_t ContinuousTaskCallbackInfo::GetCreatorUid() const
{
    return creatorUid_;
}

pid_t ContinuousTaskCallbackInfo::GetCreatorPid() const
{
    return creatorPid_;
}

std::string ContinuousTaskCallbackInfo::GetAbilityName() const
{
    return abilityName_;
}

bool ContinuousTaskCallbackInfo::IsFromWebview() const
{
    return isFromWebview_;
}

uint64_t ContinuousTaskCallbackInfo::GetTokenId() const
{
    return tokenId_;
}

void ContinuousTaskCallbackInfo::SetContinuousTaskId(const int32_t id)
{
    continuousTaskId_ = id;
}
 
void ContinuousTaskCallbackInfo::SetCancelReason(const int32_t reason)
{
    cancelReason_ = reason;
}

void ContinuousTaskCallbackInfo::SetSuspendReason(const int32_t suspendReason)
{
    suspendReason_ = suspendReason;
}

void ContinuousTaskCallbackInfo::SetSuspendState(const bool suspendState)
{
    suspendState_ = suspendState;
}
 
int32_t ContinuousTaskCallbackInfo::GetContinuousTaskId() const
{
    return continuousTaskId_;
}
 
int32_t ContinuousTaskCallbackInfo::GetCancelReason() const
{
    return cancelReason_;
}

int32_t ContinuousTaskCallbackInfo::GetSuspendReason() const
{
    return suspendReason_;
}

bool ContinuousTaskCallbackInfo::GetSuspendState() const
{
    return suspendState_;
}

bool ContinuousTaskCallbackInfo::IsByRequestObject() const
{
    return isByRequestObject_ ;
}

std::string ContinuousTaskCallbackInfo::GetBundleName() const
{
    return bundleName_;
}

int32_t ContinuousTaskCallbackInfo::GetUserId() const
{
    return userId_;
}

int32_t ContinuousTaskCallbackInfo::GetAppIndex() const
{
    return appIndex_;
}

void ContinuousTaskCallbackInfo::SetByRequestObject(const bool isByRequestObject)
{
    isByRequestObject_ = isByRequestObject;
}

void ContinuousTaskCallbackInfo::SetBundleName(const std::string &bundleName)
{
    bundleName_ = bundleName;
}

void ContinuousTaskCallbackInfo::SetUserId(int32_t userId)
{
    userId_ = userId;
}

void ContinuousTaskCallbackInfo::SetAppIndex(int32_t appIndex)
{
    appIndex_ = appIndex;
}

void ContinuousTaskCallbackInfo::SetBackgroundSubModes(
    const std::vector<uint32_t> &backgroundSubModes)
{
    backgroundSubModes_ = backgroundSubModes;
}

void ContinuousTaskCallbackInfo::SetNotificationId(int32_t notificationId)
{
    notificationId_ = notificationId;
}

void ContinuousTaskCallbackInfo::SetWantAgentBundleName(
    const std::string &wantAgentBundleName)
{
    wantAgentBundleName_ = wantAgentBundleName;
}

void ContinuousTaskCallbackInfo::SetWantAgentAbilityName(
    const std::string &wantAgentAbilityName)
{
    wantAgentAbilityName_ = wantAgentAbilityName;
}

std::vector<uint32_t> ContinuousTaskCallbackInfo::GetBackgroundSubModes() const
{
    return backgroundSubModes_;
}

int32_t ContinuousTaskCallbackInfo::GetNotificationId() const
{
    return notificationId_;
}

std::string ContinuousTaskCallbackInfo::GetWantAgentBundleName() const
{
    return wantAgentBundleName_;
}

std::string ContinuousTaskCallbackInfo::GetWantAgentAbilityName() const
{
    return wantAgentAbilityName_;
}

bool ContinuousTaskCallbackInfo::Marshalling(Parcel &parcel) const
{
    WRITE_PARCEL_WITH_RET(parcel, Uint32, typeId_, false);
    WRITE_PARCEL_WITH_RET(parcel, Int32, creatorUid_, false);
    WRITE_PARCEL_WITH_RET(parcel, Int32, creatorPid_, false);
    WRITE_PARCEL_WITH_RET(parcel, Bool, isFromWebview_, false);
    std::u16string u16AbilityName = Str8ToStr16(abilityName_);
    WRITE_PARCEL_WITH_RET(parcel, String16, u16AbilityName, false);
    WRITE_PARCEL_WITH_RET(parcel, Bool, isBatchApi_, false);
    WRITE_PARCEL_WITH_RET(parcel, UInt32Vector, typeIds_, false);
    WRITE_PARCEL_WITH_RET(parcel, Int32, abilityId_, false);
    WRITE_PARCEL_WITH_RET(parcel, Uint64, tokenId_, false);
    WRITE_PARCEL_WITH_RET(parcel, Int32, continuousTaskId_, false);
    WRITE_PARCEL_WITH_RET(parcel, Int32, cancelReason_, false);
    WRITE_PARCEL_WITH_RET(parcel, Bool, suspendState_, false);
    WRITE_PARCEL_WITH_RET(parcel, Int32, suspendReason_, false);
    WRITE_PARCEL_WITH_RET(parcel, Bool, isByRequestObject_, false);
    std::u16string u16BundleName = Str8ToStr16(bundleName_);
    WRITE_PARCEL_WITH_RET(parcel, String16, u16BundleName, false);
    WRITE_PARCEL_WITH_RET(parcel, Int32, userId_, false);
    WRITE_PARCEL_WITH_RET(parcel, Int32, appIndex_, false);
    WRITE_PARCEL_WITH_RET(parcel, UInt32Vector, backgroundSubModes_, false);
    WRITE_PARCEL_WITH_RET(parcel, Int32, notificationId_, false);
    std::u16string u16WantAgentBundleName = Str8ToStr16(wantAgentBundleName_);
    WRITE_PARCEL_WITH_RET(parcel, String16, u16WantAgentBundleName, false);
    std::u16string u16WantAgentAbilityName_ = Str8ToStr16(wantAgentAbilityName_);
    WRITE_PARCEL_WITH_RET(parcel, String16, u16WantAgentAbilityName_, false);
    return true;
}

ContinuousTaskCallbackInfo *ContinuousTaskCallbackInfo::Unmarshalling(Parcel &parcel)
{
    auto object = new (std::nothrow) ContinuousTaskCallbackInfo();
    if ((object != nullptr) && !object->ReadFromParcel(parcel)) {
        delete object;
        object = nullptr;
    }

    return object;
}

bool ContinuousTaskCallbackInfo::ReadFromParcel(Parcel &parcel)
{
    READ_PARCEL_WITH_RET(parcel, Uint32, typeId_, false);
    READ_PARCEL_WITH_RET(parcel, Int32, creatorUid_, false);
    READ_PARCEL_WITH_RET(parcel, Int32, creatorPid_, false);
    READ_PARCEL_WITH_RET(parcel, Bool, isFromWebview_, false);
    std::u16string u16AbilityName;
    READ_PARCEL_WITH_RET(parcel, String16, u16AbilityName, false);
    abilityName_ = Str16ToStr8(u16AbilityName);
    READ_PARCEL_WITH_RET(parcel, Bool, isBatchApi_, false);
    READ_PARCEL_WITH_RET(parcel, UInt32Vector, (&typeIds_), false);
    READ_PARCEL_WITH_RET(parcel, Int32, abilityId_, false);
    READ_PARCEL_WITH_RET(parcel, Uint64, tokenId_, false);
    READ_PARCEL_WITH_RET(parcel, Int32, continuousTaskId_, false);
    READ_PARCEL_WITH_RET(parcel, Int32, cancelReason_, false);
    READ_PARCEL_WITH_RET(parcel, Bool, suspendState_, false);
    READ_PARCEL_WITH_RET(parcel, Int32, suspendReason_, false);
    READ_PARCEL_WITH_RET(parcel, Bool, isByRequestObject_, false);
    std::u16string u16BundleName;
    READ_PARCEL_WITH_RET(parcel, String16, u16BundleName, false);
    bundleName_ = Str16ToStr8(u16BundleName);
    READ_PARCEL_WITH_RET(parcel, Int32, userId_, false);
    READ_PARCEL_WITH_RET(parcel, Int32, appIndex_, false);
    READ_PARCEL_WITH_RET(parcel, UInt32Vector, (&backgroundSubModes_), false);
    READ_PARCEL_WITH_RET(parcel, Int32, notificationId_, false);
    std::u16string u16WantAgentBundleName;
    READ_PARCEL_WITH_RET(parcel, String16, u16WantAgentBundleName, false);
    wantAgentBundleName_ = Str16ToStr8(u16WantAgentBundleName);
    std::u16string u16WantAgentAbilityName;
    READ_PARCEL_WITH_RET(parcel, String16, u16WantAgentAbilityName, false);
    wantAgentAbilityName_ = Str16ToStr8(u16WantAgentAbilityName);
    return true;
}
}  // namespace BackgroundTaskMgr
}  // namespace OHOS